package com.example.mystoryapp2.ui.story.create

import android.annotation.SuppressLint
import android.util.Log
import android.widget.ArrayAdapter
import androidx.lifecycle.MutableLiveData
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.AppContext
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.data.network.NetCallBack
import com.example.mystoryapp2.data.network.ServiceCreator
import com.example.mystoryapp2.data.network.model.PageModel
import com.example.mystoryapp2.data.network.model.StoryModel
import com.example.mystoryapp2.domain.Category
import com.example.mystoryapp2.domain.Page
import com.example.mystoryapp2.domain.Story
import com.example.mystoryapp2.enums.EnumEditStoryMode
import com.example.mystoryapp2.ui.base.BaseViewModel
import retrofit2.Call
import retrofit2.Response

@SuppressLint("ResourceType")
class CreateStoryViewModel : BaseViewModel() {
    private val TAG = "CreateStoryViewModel"

    var hasPage: Boolean = false

    /**
     * 双向绑定的数据需要用MutableLiveData
     * 与activity_create_story.xml中的绑定数据对应
     */
    var title = MutableLiveData<String>()

    var description = MutableLiveData<String>()

    var categoryId = MutableLiveData<Int>()

    var adapter: ArrayAdapter<String> =
        ArrayAdapter(AppContext.application.baseContext, R.id.spCategory);

    val categoryStrList: List<String>
        get() = categoryList.map { it.getCategoryName() } // 假设Category类有一个name属性

    private var categoryList: List<Category> = StoryModel.categoryList

    init {
        adapter.addAll(categoryStrList)
    }

    fun getFirstPage(b:(Int)->Unit){
        PageModel.getFirst(StoryContext.storyId,object: ServiceCreator.ICallBack<Page>{
            override fun onSuccess(res: Page) {
                b(res.pageId!!)
            }
        })
    }


    fun create(callback: ServiceCreator.ICallBack<Boolean>) {
        StoryModel.addStory(buildStory(), callback)
        Log.d(TAG, "save: ")
    }

    fun save(callback: ServiceCreator.ICallBack<Boolean>) {
        StoryModel.updateStory(buildStory(), callback)
    }

    fun initWorkMode(updateWorkMode: (EnumEditStoryMode, Boolean) -> Unit) {
        StoryModel.hasPage(StoryContext.storyId, object : ServiceCreator.ICallBack<Boolean> {
            override fun onSuccess(res: Boolean) {
                updateWorkMode(StoryContext.createActWorkMode, res)
                hasPage = res
            }
        })
    }

    fun initStory(c: (Int) -> Unit) {
        StoryModel.getStoryById(StoryContext.storyId, object : ServiceCreator.ICallBack<Story> {
            override fun onSuccess(res: Story) {
                title.value = res.getTitle()
                description.value = res.getDescription()
                categoryId.value = res.getCategoryId()
                c(StoryModel.getLocationById(categoryId.value))
            }
        })
    }


    fun onItemSelected(position: Int) {
        categoryId.value = categoryList[position].getCategoryId()
        Log.d(TAG, "onItemSelected: " + categoryId.value)
    }

    private fun buildStory(): Story {
        val _story = Story()
        if (StoryContext.createActWorkMode == EnumEditStoryMode.EDIT) {
            _story.setStoryId(StoryContext.storyId)
        }
        _story.setAuthorId(AppContext.userInfo.userId)
        _story.setTitle(title.value)
        _story.setCategoryId(categoryId.value)
        _story.setDescription(description.value)
        return _story
    }
}